// 封装分类数据业务相关代码
import { ref, watchEffect, onMounted } from 'vue';
import { getCategoryAPI } from '@/apis/category';
import { useRoute, onBeforeRouteUpdate } from 'vue-router';
export function useCategory() {
    // 获取数据
    const categoryData = ref({});
    // 获取vue路由参数
    const route = useRoute();
    const getCategory = async (id = route.params.id) => {
        const res = await getCategoryAPI(id);
        categoryData.value = res.result;
    };
    // 初始化数据
    onMounted(() => getCategory());
    // 监听路由参数变化，获取不同分类下数据
    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id);
    });
    // watchEffect(() => getCategory()); // 另外一种写法
    return {
        categoryData
    }
}